package online.duanqing.authorization.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created with hz
 *
 * @Author: DQ
 * @Date: 2024/01/16/10:57
 * @Description: 描述语句
 */

@Component
@EnableAuthorizationServer
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Resource
    private GeneralConfig generalConfig;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.allowFormAuthenticationForClients()
                .checkTokenAccess("permitAll()");
    }


    /**
     * authorization_code 授权码模式
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient(generalConfig.getClientId()) //授权服务器提供
                .secret(passwordEncoder.encode(generalConfig.getClientSecret())) //授权服务器提供
                .authorizedGrantTypes("authorization_code")
                .scopes(generalConfig.getScopes())
                .resourceIds(generalConfig.getResourceIds())
                .redirectUris("http://localhost:8080");//资源服务器提供
    }






}
